﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ItemMoveHandler : MonoBehaviour
{
    //单利
    public static ItemMoveHandler Instance { get; private set; }
    //获取拖拽物品
    private Image icon;
    //保存当前点击的物品
    private SlotData selectedSlotData;
    //角色
    //private GameObject player;
    private Player player;
    //判断当前是否按下Ctrl键(按住Ctrl键一个一个丢弃物品)
    private bool isCtrlDown;

    private void Awake()
    {
        Instance = this;
        icon = GetComponentInChildren<Image>();
        HideIcon();
        //找到角色
        //player = GameObject.FindGameObjectWithTag("Player");
        player = GameObject.FindObjectOfType<Player>();
    }
    private void Update()
    {
        //判断当前物品是否使用
        if (icon.enabled)
        {
            Vector2 position;
            //把屏幕上的点转换为本地坐标点（）
            RectTransformUtility.ScreenPointToLocalPointInRectangle(
                GetComponent<RectTransform>(), Input.mousePosition, null, out position);
            //把锚点坐标赋个icon
            icon.GetComponent<RectTransform>().anchoredPosition = position;
        }
        //丢弃物品检测（检测点击非UI事件）
        if (Input.GetMouseButtonDown(0))
        {
            if (EventSystem.current.IsPointerOverGameObject() == false)
            {
                ThrowItem();
            }
        }
        //检测Ctrl键是否按下
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            isCtrlDown = true;
        }
        if (Input.GetKeyUp(KeyCode.LeftControl))
        {
            isCtrlDown = false;
        }
        //取消放置物品操作
        if (Input.GetMouseButtonDown(1))
        {
            ClearHandForced();
        }
    }

    //点击
    public void OnSlotClick(SlotUI slotui)
    {
        //print("onClick" + slotui);
        //判断手上是否有无物品
        if (selectedSlotData != null)
        {
            //1-不为空
            //1-1当前点击了一个空格子，把物品放入该格子
            if (slotui.GetData().IsEmpty())
            {
                MoveToEmptySlot(selectedSlotData, slotui.GetData());
            }
            else
            {
                //1-2当前点击了一个非空格子
                //1-2-1点击的是自己
                if (selectedSlotData == slotui.GetData()) return;
                else
                {
                    //1-2-2类型一致，数量++；
                    if (selectedSlotData.item == slotui.GetData().item)
                    {
                        MoveToNotEmptySlot(selectedSlotData, slotui.GetData());
                    }
                    else
                    {
                        //1-2-3类型不一致，物品交换
                        SwitchData(selectedSlotData, slotui.GetData());
                    }
                }
            }
        }
        else
        {
            //2-手上为空
            if (slotui.GetData().IsEmpty()) return;
            selectedSlotData = slotui.GetData();
            ShowIcon(selectedSlotData.item.sprite);
        }
    }
    //拖拽的物品显示或隐藏
    void HideIcon()
    {
        icon.enabled = false;
    }
    //更新显示的物品
    void ShowIcon(Sprite sprite)
    {
        icon.sprite = sprite;
        icon.enabled = true;
    }
    //丢弃物品
    private void ThrowItem()
    {
        if (selectedSlotData != null)
        {
            print("Throw");
            GameObject prefab = selectedSlotData.item.prefab;
            int count = selectedSlotData.count;
            if (isCtrlDown)
            {
                player.ThrowItem(prefab, 1);
                selectedSlotData.Reduce();
            }
            else
            {
                player.ThrowItem(prefab, count);
                selectedSlotData.Clear();
            }
            ClearHand();
        }
    }
    //清空手上所有丢弃的物品
    void ClearHand()
    {
        if (selectedSlotData.IsEmpty())
        {
            HideIcon();
            selectedSlotData = null;
        }
    }
    //强制清空
    void ClearHandForced()
    {
        HideIcon();
        selectedSlotData = null;
    }
    //移动物品到其他空格子,默认移动所有
    private void MoveToEmptySlot(SlotData fromData, SlotData toData)
    {
        if (isCtrlDown)
        {
            toData.AddItem(fromData.item);
            fromData.Reduce();
        }
        else
        {
            toData.MoveSlot(fromData);
            fromData.Clear();
        }
        ClearHand();
    }
    //移动物品到其他非空格子,默认移动所有
    private void MoveToNotEmptySlot(SlotData fromData, SlotData toData)
    {
        if (isCtrlDown)
        {
            //<99不能添加
            if (toData.CanAddItem())
            {
                toData.Add();
                fromData.Reduce();
            }
        }
        else
        {
            int freespace = toData.GetFreeSpace();
            //当移动的数量超过最大数值，可以添加最大的额度
            if (fromData.count > freespace)
            {
                toData.Add(freespace);
                fromData.Reduce(freespace);
            }
            else
            {
                //容量够的情况
                toData.Add(fromData.count);
                fromData.Clear();
            }
        }
        ClearHand();
    }
    //交换物品的位置
    private void SwitchData(SlotData data1, SlotData data2)
    {
        //先保存物品1的数据，在交换
        ItemData item = data1.item;
        int count = data1.count;
        data1.MoveSlot(data2);
        data2.AddItem(item,count);
        ClearHandForced();
    }
}
